using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

using CStock;
using CUtil;

namespace CapitalReSources
{
	public enum TradeStatisticType
	{
		All,
		PerformanceBuyAndHold,
		Performance,
		PerformanceRelative

	}

	/// <summary>
	/// Summary description for TradeStatitics.
	/// </summary>
	public class TradeStatistics : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Splitter splitter1;
		private System.Windows.Forms.ToolBar toolBar1;
        private CGrid.GridBase grid1;
		private System.Windows.Forms.Splitter splitter2;
		private System.Windows.Forms.Panel panel1;
		private EForms.EButton buttonOK;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.ToolBarButton toolBarButtonShareOrPortfolio;
		private System.Windows.Forms.ToolBarButton toolBarButtonPerformance;
		private System.Windows.Forms.ToolBarButton toolBarButtonPerformanceBuyAndHold_Share;
		private System.Windows.Forms.ToolBarButton toolBarButton1;
		private System.Windows.Forms.ToolBarButton toolBarButtonPerformanceRelative;
		private System.ComponentModel.IContainer components;

		private IPortfolio portfolio;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.StatusBar statusBar1;
		private System.Windows.Forms.StatusBarPanel statusBarPanel1;
		private System.Windows.Forms.StatusBarPanel statusBarPanel2;
		private System.Windows.Forms.ToolBarButton toolBarButtonAllPerformances;
		TradeStatisticType typeStatistics;
		


//		public TradeStatistics(ITradeStep tradeStep)
//		{
//			InitializeComponent();
//			this.grid1.CreateGrid(CUtil.GridTypes.TradeStatistics);
//	
//			
//			FillGrid(tradeStep);
//
//		}
//		public TradeStatistics(IDataClass dataClass)
//		{
//			NameObjectCollection performancePerYear = dataClass.PerformancePerYearCollection;
//            
//			InitializeComponent();
//			if(performancePerYear != null)
//			{
//				this.grid1.CreateGrid(performancePerYear);
//			
//				FillGrid(performancePerYear);
//			}
//		}
		public TradeStatistics(IPortfolio port, TradeStatisticType typeStat)
		{
			InitializeComponent();
			portfolio = port;
			typeStatistics = typeStat;
			CreateGridPerformancePortfolio();

			
           
			
			
		}
		private void RefreshView()
		{
			this.grid1.ClearGridRows();
			this.AddPortfolioItem();

		}

		private void AddPerformanceToGrid(string entry, NameObjectCollection performancePerYear)
		{
			

			if(performancePerYear != null && performancePerYear.Count > 0)
			{
				
				this.grid1.AddRowsFromCollection(entry, performancePerYear);
				
			}

		}
		private void AddDataClassItem(IDataItem di, bool clearGrid)
		{

            ITradeStep myTradeStep = di.TradeStep;

			
			if(clearGrid)
                this.grid1.ClearGridRows();
			switch (this.typeStatistics)
			{
				case TradeStatisticType.PerformanceBuyAndHold:
				{
                    AddPerformanceToGrid(di.Name + " BuyAndHold", myTradeStep.PerformanceBuyAndHoldPerYearCollection);
					break;
				}
				case TradeStatisticType.Performance:
				{
                    AddPerformanceToGrid(di.Name + " TradeSystem", myTradeStep.PerformancePerYearCollection);
					break;
				}
				case TradeStatisticType.PerformanceRelative:
				{
                    AddPerformanceToGrid(di.Name + " Relative", myTradeStep.PerformanceRelativePerYearCollection);
					
					break;
				}
				case TradeStatisticType.All:
				{
                    AddPerformanceToGrid(di.Name + " BuyAndHold", myTradeStep.PerformanceBuyAndHoldPerYearCollection);
                    AddPerformanceToGrid(di.Name + " TradeSystem", myTradeStep.PerformancePerYearCollection);
                    AddPerformanceToGrid(di.Name + " Relative", myTradeStep.PerformanceRelativePerYearCollection);
					
					
					break;
				}
			}

	
			
		}
		private void CreateGridPerformanceBuyAndHold_Share()
		{
			IDataItem di = portfolio.DataItem_Selected;
            this.grid1.CreateGrid(di.TradeStep.PerformancePerYearCollection);
			AddDataClassItem(di , true);

			
		}
		private void CreateGridPerformancePortfolio()
		{
		
			this.grid1.CreateGrid(this.portfolio.TradeStepPortfolio.PerformancePerYearCollection);
			AddPortfolioItem();
			
		}
		private void AddPortfolioItem()
		{
			this.grid1.ClearGridRows();

            ITradeStep myTradeStep = this.portfolio.TradeStepPortfolio;

			
			for(int i = 0; i < this.portfolio.DataItems.Count; i++)
			{
				IDataItem di = this.portfolio.DataItems[i];
				AddDataClassItem( di, false);
			}

			//NameObjectCollection performancePerYear = null;
			switch (this.typeStatistics)
			{
				case TradeStatisticType.PerformanceBuyAndHold:
				{
                    AddPerformanceToGrid(portfolio.Name + " BuyAndHold", myTradeStep.PerformanceBuyAndHoldPerYearCollection);
					break;
				}
				case TradeStatisticType.Performance:
				{
                    AddPerformanceToGrid(portfolio.Name + " TradeSystem", myTradeStep.PerformancePerYearCollection);
					
					break;
				}
				case TradeStatisticType.PerformanceRelative:
				{
                    AddPerformanceToGrid(portfolio.Name + " Relative", myTradeStep.PerformanceRelativePerYearCollection);
					
					break;
				}
				case TradeStatisticType.All:
				{
                    AddPerformanceToGrid(portfolio.Name + " BuyAndHold", myTradeStep.PerformanceBuyAndHoldPerYearCollection);

                    AddPerformanceToGrid(portfolio.Name + " TradeSystem", myTradeStep.PerformancePerYearCollection);
                    AddPerformanceToGrid(portfolio.Name + " Relative", myTradeStep.PerformanceRelativePerYearCollection);
					break;
				}
			}

//			if(performancePerYear != null && performancePerYear.Count > 0)
//			{
//				
//			}
			
				
			
		}
		#region add grid line

		
		public void FillGrid(ITradeStep ts)
		{
			
//			AddNewRow();
//			SourceGrid2.Cell c;
//			int rowListCount = grid1.Rows -1;
//			
//			c = grid1[rowListCount,0];
//			c.Value = "Performance per year";
//			c= grid1[rowListCount,1];
//			c.Value = ts.PerformancePerYear.ToString();
//
//			AddNewRow();
//			rowListCount = grid1.Rows -1;
//			c = grid1[rowListCount,0];
//			c.Value = "Number of trades";
//			c= grid1[rowListCount,1];
//			c.Value = ts.TradeEvents.Count.ToString();
//
//			AddNewRow();
//			rowListCount = grid1.Rows -1;
//			c = grid1[rowListCount,0];
//			c.Value = "Bank costs";
//			c= grid1[rowListCount,1];
//			c.Value = ts.TotalBankingCosts.ToString();

	



		}
	
	

		#endregion

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.splitter1 = new System.Windows.Forms.Splitter();
			this.toolBar1 = new System.Windows.Forms.ToolBar();
			this.toolBarButtonShareOrPortfolio = new System.Windows.Forms.ToolBarButton();
			this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonPerformance = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonPerformanceBuyAndHold_Share = new System.Windows.Forms.ToolBarButton();
			this.toolBarButtonPerformanceRelative = new System.Windows.Forms.ToolBarButton();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
            this.grid1 = new CGrid.GridBase();
			this.splitter2 = new System.Windows.Forms.Splitter();
			this.panel1 = new System.Windows.Forms.Panel();
			this.statusBar1 = new System.Windows.Forms.StatusBar();
			this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
			this.statusBarPanel2 = new System.Windows.Forms.StatusBarPanel();
			this.buttonOK = new EForms.EButton();
			this.mainMenu1 = new System.Windows.Forms.MainMenu();
			this.toolBarButtonAllPerformances = new System.Windows.Forms.ToolBarButton();
			this.panel1.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).BeginInit();
			this.SuspendLayout();
			// 
			// splitter1
			// 
			this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
			this.splitter1.Location = new System.Drawing.Point(0, 0);
			this.splitter1.Name = "splitter1";
			this.splitter1.Size = new System.Drawing.Size(696, 3);
			this.splitter1.TabIndex = 42;
			this.splitter1.TabStop = false;
			// 
			// toolBar1
			// 
			this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
																						this.toolBarButtonShareOrPortfolio,
																						this.toolBarButton1,
																						this.toolBarButtonAllPerformances,
																						this.toolBarButtonPerformance,
																						this.toolBarButtonPerformanceBuyAndHold_Share,
																						this.toolBarButtonPerformanceRelative});
			this.toolBar1.DropDownArrows = true;
			this.toolBar1.ImageList = this.imageList1;
			this.toolBar1.Location = new System.Drawing.Point(0, 3);
			this.toolBar1.Name = "toolBar1";
			this.toolBar1.ShowToolTips = true;
			this.toolBar1.Size = new System.Drawing.Size(696, 42);
			this.toolBar1.TabIndex = 44;
			this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
			// 
			// toolBarButtonShareOrPortfolio
			// 
			this.toolBarButtonShareOrPortfolio.ImageIndex = 0;
			this.toolBarButtonShareOrPortfolio.Style = System.Windows.Forms.ToolBarButtonStyle.ToggleButton;
			// 
			// toolBarButton1
			// 
			this.toolBarButton1.Style = System.Windows.Forms.ToolBarButtonStyle.Separator;
			// 
			// toolBarButtonPerformance
			// 
			this.toolBarButtonPerformance.ImageIndex = 1;
			this.toolBarButtonPerformance.Text = "P";
			// 
			// toolBarButtonPerformanceBuyAndHold_Share
			// 
			this.toolBarButtonPerformanceBuyAndHold_Share.ImageIndex = 2;
			this.toolBarButtonPerformanceBuyAndHold_Share.Text = "S";
			// 
			// toolBarButtonPerformanceRelative
			// 
			this.toolBarButtonPerformanceRelative.ImageIndex = 3;
			this.toolBarButtonPerformanceRelative.Text = "R";
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// grid1
			// 
			
			this.grid1.ProgramContext = null;
			this.grid1.Dock = System.Windows.Forms.DockStyle.Top;
			this.grid1.Location = new System.Drawing.Point(0, 45);
			this.grid1.Name = "grid1";
			this.grid1.Size = new System.Drawing.Size(696, 320);
			this.grid1.TabIndex = 45;
			// 
			// splitter2
			// 
			this.splitter2.Dock = System.Windows.Forms.DockStyle.Top;
			this.splitter2.Location = new System.Drawing.Point(0, 365);
			this.splitter2.Name = "splitter2";
			this.splitter2.Size = new System.Drawing.Size(696, 8);
			this.splitter2.TabIndex = 46;
			this.splitter2.TabStop = false;
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.statusBar1);
			this.panel1.Controls.Add(this.buttonOK);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.panel1.Location = new System.Drawing.Point(0, 373);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(696, 96);
			this.panel1.TabIndex = 47;
			// 
			// statusBar1
			// 
			this.statusBar1.Location = new System.Drawing.Point(0, 74);
			this.statusBar1.Name = "statusBar1";
			this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
																						  this.statusBarPanel1,
																						  this.statusBarPanel2});
			this.statusBar1.Size = new System.Drawing.Size(696, 22);
			this.statusBar1.TabIndex = 42;
			// 
			// statusBarPanel1
			// 
			this.statusBarPanel1.Text = "UUU";
			// 
			// statusBarPanel2
			// 
			this.statusBarPanel2.Text = "NNN";
			// 
			// buttonOK
			// 
			this.buttonOK.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(212)), ((System.Byte)(208)), ((System.Byte)(200)));
			this.buttonOK.DialogResult = System.Windows.Forms.DialogResult.None;
			this.buttonOK.Font = new System.Drawing.Font("Verdana", 9F);
			this.buttonOK.Location = new System.Drawing.Point(300, 36);
			this.buttonOK.Name = "buttonOK";
			this.buttonOK.Size = new System.Drawing.Size(96, 24);
			this.buttonOK.TabIndex = 41;
			this.buttonOK.Text = "OK";
			this.buttonOK.Click += new System.EventHandler(this.buttonOK_Click);
			// 
			// toolBarButtonAllPerformances
			// 
			this.toolBarButtonAllPerformances.Text = "A";
			// 
			// TradeStatistics
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(696, 469);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.splitter2);
			this.Controls.Add(this.grid1);
			this.Controls.Add(this.toolBar1);
			this.Controls.Add(this.splitter1);
			this.Menu = this.mainMenu1;
			this.Name = "TradeStatistics";
			this.Text = "TradeStatistics";
			this.panel1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statusBarPanel2)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion
	
		#region grid creation


		#endregion

		private void buttonOK_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
		{

			if(e.Button == this.toolBarButtonPerformanceRelative)
			{
				this.typeStatistics = TradeStatisticType.PerformanceRelative;
				

			}
			else if(e.Button == this.toolBarButtonPerformanceBuyAndHold_Share)
			{
				this.typeStatistics = TradeStatisticType.PerformanceBuyAndHold;
				

			}
			else if(e.Button == this.toolBarButtonPerformance)
			{
				this.typeStatistics = TradeStatisticType.Performance;
				

			}

			this.RefreshView();
		
		}

		


	}
}
